package com.fishery.base.service.impl;

import com.fishery.base.entity.Base;
import com.fishery.base.entity.client.BaseTypeVo;
import com.fishery.base.entity.vo.CreateBaseVo;
import com.fishery.base.mapper.BaseMapper;
import com.fishery.base.service.BaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.base.service.client.CustomizationClient;
import com.fishery.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@Service
public class BaseServiceImpl extends ServiceImpl<BaseMapper, Base> implements BaseService {

	@Autowired
	private BaseMapper baseMapper;

	@Autowired
	private CustomizationClient customizationClient;

	@Override
	public String establishBase(CreateBaseVo createBaseVo) {
		// 填充属性
		createBaseVo.setId(String.valueOf(new IdWorker().nextId()));
		// 将数据插入到数据库
		int ret = baseMapper.insertBaseByCreateBaseVo(createBaseVo);
		// 插入失败
		if (ret <= 0) {
			return null;
		}
		// 生成基地对应的标签
		BaseTypeVo baseTypeVo = new BaseTypeVo();
		baseTypeVo.setTypes(createBaseVo.getTypes());
		baseTypeVo.setBaseId(createBaseVo.getId());
		boolean flag = customizationClient.generateLabels(baseTypeVo).isFlag();
		// 生成标签失败，删除原来插入到记录
		if (!flag) {
			baseMapper.deleteById(baseTypeVo.getBaseId());
		}
		return flag ? createBaseVo.getId() : null;
	}
}
